package br.com.academia.DAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import br.com.academia.beans.Empresa;

public class EmpresaDAO {
	private Connection connection;
	
	public EmpresaDAO(Connection connection) {
		this.connection = connection;
	}
	
	public List<Empresa> buscaEmpresasSelecao() {
		List<Empresa> listaEmpresa = new ArrayList<Empresa>();
		String query = "Select idEmpresa, nomeFantasia from Empresas";

		try {
			PreparedStatement stm = connection.prepareStatement(query);
			ResultSet rs = stm.executeQuery();

			while (rs.next()) {
				Empresa empresa = new Empresa();
				empresa.setIdEmpresa(rs.getInt("idEmpresa"));
				empresa.setNomeFantasia(rs.getString("nomeFantasia"));
				listaEmpresa.add(empresa);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return listaEmpresa;
	}
	
	public List<String[]> consultaEmpresas(List<String> dadosConsulta)  {
		List<String[]> retorno = new ArrayList<String[]>();
		String query = "Select * from Empresas";
		 try {
			 PreparedStatement stm = connection.prepareStatement(query);
			 ResultSet rs = stm.executeQuery();
			 while(rs.next()) {
				 String[] row = new String[dadosConsulta.size()];
				 for(int i = 0; i < row.length; i++) {
					 row[i] = rs.getString(dadosConsulta.get(i));
				 }
				 retorno.add(row);
			 }
		 } catch(SQLException e) {
			 e.printStackTrace();
		 }
		return retorno;
	}
	
	public Empresa consultaEmpresaId(int idEmpresa) {
		Empresa empresa = new Empresa();
		String query = "Select * from Empresas where idEmpresa = ?";
		try {
			PreparedStatement stm = connection.prepareStatement(query);
			stm.setInt(1, idEmpresa);
			ResultSet rs = stm.executeQuery();
			
			if(rs.next()) {
				empresa.setIdEmpresa(rs.getInt("idEmpresa"));
				empresa.setRazaoSocial(rs.getString("razaoSocial"));
				empresa.setCnpj(rs.getInt("cnpj"));
				empresa.setIe(rs.getString("ie"));
				empresa.setNomeFantasia(rs.getString("nomeFantasia"));
			}
		}catch(SQLException e) {
			e.printStackTrace();
		}
		
		return empresa;
	}
	
	

}
